/*
Remdator v5.0.0
http://code.google.com/p/remdator/

Copyright (c) 2012 Marc Tanti (www.marctanti.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

;(function ($, undefined) {
if (typeof($.remdator) == "undefined")
{
	$.remdator = {};
}

/***********************************/
//Error to throw when an argument is missing.
/***********************************/
function ArgMissingError($args)
//Argument missing exception.
//$args: {
//	message: The message of the exception.
//}
{
	if (typeof($args) == "undefined")
	{
		throw new Error("Missing arguments.");
	}
	
	if (typeof($args.message) == "undefined")
	{
		throw new Error("Missing arguments.");
	}
	
	this.message = $args.message;
	
	if (console && console.trace)
	{
		console.trace();
	}
}
ArgMissingError.prototype = new Error();

function assertArgPresent($args)
//Assert that an argument is present.
//$args: {
//	arg: The argument to check.
//	name: The string name of the argument.
//}
{
	if (typeof($args) == "undefined")
	{
		throw new Error("Missing arguments.");
	}
	
	if (typeof($args.name) == "undefined")
	{
		throw new Error("Missing arguments.");
	}
	
	if (typeof($args.arg) == "undefined")
	{
		throw new $.remdator.ArgMissingError({ message : $args.name + " is missing." });
	}
}



/***********************************/
//Error to throw when an argument is of the wrong type.
/***********************************/
function ArgTypeError($args)
//Argument of invalid type exception.
//$args: {
//	message: The message of the exception.
//	arg: The invalid argument.
//}
{
	if (typeof($args) == "undefined")
	{
		throw new Error("Missing arguments.");
	}
	
	if (typeof($args.message) == "undefined")
	{
		throw new Error("Missing arguments.");
	}
	
	if (typeof($args.arg) == "undefined")
	{
		throw new Error("Missing arguments.");
	}
	
    this.message = $args.message;
	this.arg = $args.arg;
	
	if (console && console.trace)
	{
		console.trace();
	}
}
ArgTypeError.prototype = new Error();

function assertArgType($args)
//Assert that an argument is of the right data type.
//$args: {
//	arg: The argument to check.
//	name: The string name of the argument.
//	type: The correct type of the argument as a string.
//	orUndefined: If defined and true will not raise an exception if arg is undefined.
//}
{
	if (typeof($args) == "undefined")
	{
		throw new Error("Missing arguments.");
	}
	
	if (typeof($args.name) == "undefined")
	{
		throw new Error("Missing arguments.");
	}
	
	if (typeof($args.type) == "undefined")
	{
		throw new Error("Missing arguments.");
	}
	
	if ($args.orUndefined && typeof($args.arg) == "undefined")
	{
		return;
	}
	else
	{
		$.remdator.assertArgPresent($args);
		if (typeof($args.arg) != $args.type)
		{
			throw new $.remdator.ArgTypeError({ message : $args.name + " is not " + $args.type + ".", arg : $args.arg });
		}
	}
}



/***********************************/
//Error to throw when a general exception is to be thrown.
/***********************************/
function GeneralError($args)
//General exception.
//$args: {
//	message: The message of the exception.
//}
{
	if (typeof($args) == "undefined")
	{
		throw new Error("Missing arguments.");
	}
	
	if (typeof($args.message) == "undefined")
	{
		throw new Error("Missing arguments.");
	}
	
	this.message = $args.message;
	
	if (console && console.trace)
	{
		console.trace();
	}
}
GeneralError.prototype = new Error();



/***********************************/
//Error to throw in the outputToArgs functions if the remote script in an ajax request returns an error.
/***********************************/
function RemoteError($args)
//Remote script exception.
//$args: {
//	message: The message of the exception.
//	data: The data associated with the exception (such as an invalid form field).
//}
{
	$.remdator.assertArgType({ arg : $args, name : "arguments", type : "object" });
	$.remdator.assertArgType({ arg : $args.message, name : "message", type : "string" });
	
	this.message = $args.message;
	if (typeof($args.data) == "undefined")
	{
		this.data = null;
	}
	else
	{
		this.data = $args.data;
	}
}
RemoteError.prototype = new Error();

//Add to remdator namespace in jQuery.
$.remdator.ArgTypeError = ArgTypeError;
$.remdator.assertArgPresent = assertArgPresent;
$.remdator.ArgMissingError = ArgMissingError;
$.remdator.assertArgType = assertArgType;
$.remdator.GeneralError = GeneralError;
$.remdator.RemoteError = RemoteError;
})(jQuery);